﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    [ObjectEvent]
    public class SpellCardComponentEvent : ObjectEvent<SpellCardComponent>, IAwake
    {
        public void Awake()
        {
            Get().Awake();
        }
    }
    public class SpellCardComponent : CardUseComponent, ICardUse,IIfCardUse
    {
        public int Power;
        public int Cost;
        public int Gorgeous;
        public CharactorType charactorType;
        public DamageTypeEnum damageType;

        public override string UseInfo { get; set; } = CardUseWay.UseAsSpellCard;

        public override void Use(FightModel fightModel, BattlePlayer user)
        {
            BattleStatus battleStatus = fightModel.BattleStatus;
            if (battleStatus == BattleStatus.Battle)
            {
                UseSpellCard(fightModel, user);
            }
            if (battleStatus == BattleStatus.BattleDefence)
            {
                UseSpellCard(fightModel, user);               
            }
        }

        async void UseSpellCard(FightModel fightModel, BattlePlayer user)
        {
            PowerUseInfo powerUseInfo = new PowerUseInfo()
            {
                battlePlayer = user,
                PowerUse = Cost,
                CallBack = async () =>
                {
                    BattleCard card = baseCard;
                    user[CardPlace.Hand].MoveCards(user[CardPlace.Duel], new List<BattleCard>() { card });
                    card.IfRightSide = true;
                    if (fightModel.BattleStatus == BattleStatus.Battle)
                    {
                        Log.Debug($"攻击宣言:{card.CardName}");
                        FightCalComponent.Single.damageInfo = new DamageInfo()
                        {
                            AttackCard = card,
                            DamageType = card.GetComponent<SpellCardComponent>().damageType,
                            DamageValue = 0,
                        };
                        await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.TakeAttack, fightModel, FightCalComponent.Single.damageInfo);
                        fightModel.BattleStatus = BattleStatus.BattleDefence;
                        if (!FightCalComponent.Single.damageInfo.CanBeDefence)//被标记为贯通特性的卡牌不询问防御
                        {
                            FightCalComponent.Single.DontDefence(fightModel);
                        }
                    }
                    else
                    {
                        Log.Debug($"防御宣言:{card?.CardName}");
                        FightCalComponent.Single.damageInfo.DefenceCard = card;
                        await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.TakeDefence, fightModel, FightCalComponent.Single.damageInfo);
                        fightModel.BattleStatus = BattleStatus.Battle;
                        Log.Debug($"开始战斗结算");
                        await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.FightStart, fightModel, FightCalComponent.Single.damageInfo);
                    }
                    await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.FreshUI, fightModel);
                },
            };
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.ShowWindow, fightModel, WindowType.PowerUse, powerUseInfo);
        }



        public override bool IfUse(FightModel fightModel, BattlePlayer user)
        {
            BattleStatus battleStatus = fightModel.BattleStatus;
            if (battleStatus == BattleStatus.Battle && user == fightModel.nowPlayer&&Gorgeous>=user.GetGorgeous())
            {
                foreach (var a in user[CardPlace.Charactor].Cards)
                {
                    if (a.GetComponent<CharactorCardComponent>().charactorType==charactorType)
                        return true;
                }
                if (user[CardPlace.Duel_Charactor].Cards.Count != 0 && user[CardPlace.Duel_Charactor].Cards[0].GetComponent<CharactorCardComponent>().charactorType == charactorType)
                    return true;
            }
            if (battleStatus == BattleStatus.BattleDefence && user != fightModel.nowPlayer)
            {
                foreach (var a in user[CardPlace.Charactor].Cards)
                {
                    if (a.GetComponent<CharactorCardComponent>().charactorType == charactorType)
                        return true;
                }
                if (user[CardPlace.Duel_Charactor].Cards.Count != 0 && user[CardPlace.Duel_Charactor].Cards[0].GetComponent<CharactorCardComponent>().charactorType == charactorType)
                    return true;
            }
            return false;
        }
    }
}
